package com.duobaoyu.dby.supply.biz.client.boundary.channel;

import com.duobaoyu.dby.channel.boundary.bean.channel.ChannelAfterSaleAttributesResponseParam;
import com.duobaoyu.dby.channel.boundary.bean.order.param.OrderInfoParam;
import com.duobaoyu.dby.channel.boundary.bean.order.param.OutboundOrderParam;
import com.duobaoyu.dby.channel.boundary.bean.product.param.ProductSkuDetailParam;
import com.duobaoyu.dby.channel.boundary.bean.refund.param.AfterSaleParticularParam;
import com.duobaoyu.dby.channel.boundary.template.SupplyChannelTemplate;
import com.duobaoyu.dby.common.core.constant.SecurityConstants;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.saas.product.api.feign.RemoteSaasProductSkuStockApiService;
import com.duobaoyu.dby.saas.product.common.remote.to.ProductTO;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderBO;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderProductBO;
import com.duobaoyu.dby.supply.biz.util.RemoteResultCheckUtil;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleOutBO;
import com.duobaoyu.dby.supply.core.common.exception.CustomAssert;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 渠道工具客户端
 * @author chenms
 * @date 2023/11/27 9:25 AM
 */
@Component
@RequiredArgsConstructor
public class SupplyChainChannelToolClient {

    private final SupplyChannelTemplate supplyChannelTemplate;
    private final RemoteSaasProductSkuStockApiService remoteSaasProductSkuStockApiService;

    /**
     * 获取渠道订单明细响应数据
     */
    public Object getChannelOrderInfoResponse(OrderBO order) {
        OrderInfoParam orderInfoParam = new OrderInfoParam();
        orderInfoParam.setOrderTradeSn(order.getOrderTradeSn());
        orderInfoParam.setChannelCode(order.getChannelCode());
        orderInfoParam.setChannelOrderSn(order.getChannelOrderSn());
        orderInfoParam.setChannelOrderParentSn(order.getChannelOrderParentSn());
        orderInfoParam.setChannelTradeOrderSn(order.getChannelTradeOrderSn());
        orderInfoParam.setOriginalPrice(Optional.ofNullable(order.getOrderProductList()).flatMap(productList -> productList.stream().findFirst().map(OrderProductBO::getOriginalPrice)).orElse(null));
        orderInfoParam.setOrderProductTypeSet(Sets.newHashSet(order.getOrderType()));
        return supplyChannelTemplate.channelOrderDetailResponse(orderInfoParam);
    }


    /**
     * 获取渠道出库单响应数据
     */
    public Object getChannelOutboundOrderResponse(OrderBO order) {
        OutboundOrderParam outboundOrderParam = new OutboundOrderParam();
        outboundOrderParam.setChannelCode(order.getChannelCode());
        outboundOrderParam.setChannelTradeOrderSn(order.getChannelTradeOrderSn());
        outboundOrderParam.setChannelPackageId(order.getChannelOrderSn());
        return supplyChannelTemplate.channelOutboundOrderResponse(outboundOrderParam);
    }

    /**
     * 获取渠道售后明细响应数据
     */
    public Object getChannelAfterSaleDetailResponse(AfterSaleOutBO afterSale) {
        AfterSaleParticularParam afterSaleParticularParam = new AfterSaleParticularParam();
        afterSaleParticularParam.setChannelCode(afterSale.getChannelCode());
        afterSaleParticularParam.setAfterSaleSn(afterSale.getAfterSaleSn());
        afterSaleParticularParam.setAfterSaleTradeSn(afterSale.getAfterSaleTradeOrderSn());
        afterSaleParticularParam.setChannelAfterSaleSn(afterSale.getChannelAfterSaleSn());
        afterSaleParticularParam.setChannelOrderSn(afterSale.getChannelOrderSn());
        return supplyChannelTemplate.channelAfterSaleDetailResponse(afterSaleParticularParam);
    }

    /**
     * 获取渠道售后权益响应数据
     */
    public Object getChannelAfterSaleAttributesResponse(OrderBO order, Integer afterSaleType, Integer goodsStatus) {
        ChannelAfterSaleAttributesResponseParam channelAfterSaleAttributesResponseParam = new ChannelAfterSaleAttributesResponseParam();
        channelAfterSaleAttributesResponseParam.setChannelCode(order.getChannelCode());
        channelAfterSaleAttributesResponseParam.setChannelTradeOrderSn(order.getChannelTradeOrderSn());
        channelAfterSaleAttributesResponseParam.setChannelOrderSn(order.getChannelOrderSn());
        channelAfterSaleAttributesResponseParam.setChannelPreOrderSn(order.getChannelOrderParentSn());
        channelAfterSaleAttributesResponseParam.setOrderTradeSn(order.getOrderTradeSn());
        channelAfterSaleAttributesResponseParam.setAfterSaleType(afterSaleType);
        List<ChannelAfterSaleAttributesResponseParam.Sku> skuList = Optional.ofNullable(order.getOrderProductList()).orElse(Collections.emptyList()).stream().map(orderProduct -> {
            ChannelAfterSaleAttributesResponseParam.Sku sku = new ChannelAfterSaleAttributesResponseParam.Sku();
            sku.setSkuCode(orderProduct.getSkuCode());
            sku.setChannelSkuCode(orderProduct.getChannelSkuCode());
            sku.setGoodsStatus(goodsStatus);
            return sku;
        }).collect(Collectors.toList());
        channelAfterSaleAttributesResponseParam.setSkuList(skuList);

        return supplyChannelTemplate.channelAfterSaleAttributesResponse(channelAfterSaleAttributesResponseParam);
    }

    /**
     * 获取渠道商品明细响应数据
     */
    public Object getChannelProductSkuDetailResponse(String skuCode) {
        R<ProductTO> productResponse = remoteSaasProductSkuStockApiService.skuDetail(skuCode, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(productResponse, null, skuCode);
        ProductTO product = productResponse.getData();
        CustomAssert.notNull(product, "商品不存在");
        CustomAssert.notEmpty(product.getSkuInfos(), "商品无SKU信息");
        ProductTO.SkuInfo skuInfo = product.getSkuInfos().stream().filter(item -> Objects.equals(skuCode, item.getSkuCode())).findFirst().orElse(null);
        CustomAssert.notNull(skuInfo, "SKU不存在");
        ProductSkuDetailParam productSkuDetailParam = new ProductSkuDetailParam();
        productSkuDetailParam.setChannelCode(skuInfo.getChannelCode());
        ProductSkuDetailParam.ProductSkuDetailParamData productSkuDetailParamData = new ProductSkuDetailParam.ProductSkuDetailParamData();
        productSkuDetailParamData.setChannelSkuCode(skuInfo.getChannelSkuCode());
        productSkuDetailParamData.setChannelOriginalSkuCode(skuInfo.getChannelOriginalSkuCode());
        productSkuDetailParamData.setChannelProductCode(product.getChannelProductCode());
        productSkuDetailParamData.setChannelSkuCode(skuInfo.getChannelSkuCode());
        productSkuDetailParam.setData(Lists.newArrayList(productSkuDetailParamData));
        productSkuDetailParam.setEnableChannelResponse(true);
        return supplyChannelTemplate.channelProductSkuDetailResponse(productSkuDetailParam);
    }
}
